#crud
from sprox.tablebase import TableBase
from sprox.formbase import EditableForm, AddRecordForm
from sprox.fillerbase import TableFiller, EditFormFiller
#turbogears
from tgext.crud import CrudRestController
from tg.controllers import RestController
from tg import expose,require, flash
from tg.controllers import redirect
from tg.decorators import validate

import formencode
from formencode import validators
from repoze.what import predicates
from pylons.i18n import ugettext as _
#model
from pydive.model import DBSession, metadata
from pydive.model.auth import User,Group
from pydive.model.dive import Diver

class DiverController(CrudRestController):
    model = Diver
    #@expose('tgext.crud.templates.get_all')
    @require(predicates.has_permission('manage', msg=_('You have no permission')))
    @expose('pydive.templates.news.get_all')
    def get_all(self, *args, **kw):
        return super(DiverController, self).get_all(*args, **kw)
    @require(predicates.has_permission('manage', msg=_('You have no permission')))
    @expose('tgext.crud.templates.new')
    def new(self, *args, **kw):
        return super(DiverController, self).new(*args, **kw)
    @require(predicates.has_permission('manage', msg=_('You have no permission')))
    @expose('tgext.crud.templates.get')
    def get(self, *args, **kw):
        return super(DiverController, self).new(*args, **kw)
    
    @require(predicates.has_permission('manage', msg=_('You have no permission')))
    @expose()
    def post(self, *args, **kw):
        try:
            super(DiverController, self).post(*args, **kw)
        except:
            redirect('/')
    
    @require(predicates.has_permission('manage', msg=_('You have no permission')))
    @expose('tgext.crud.templates.edit')
    def edit(self, *args, **kw):
        return super(DiverController, self).edit(*args, **kw)
    
    @require(predicates.has_permission('manage', msg=_('You have no permission')))
    @expose()
    def put(self, *args, **kw):
        try:
            super(DiverController, self).put(*args, **kw)
        except:
            redirect('/')
        
    @expose('pydive.templates.diver.buddyIcon')
    def buddyIcon(self):
            return dict()
    

    class new_form_type(AddRecordForm):
        __model__ = Diver
        __omit_fields__ = ['id','author_id']
       # __omit_fields__ = ['user_id', 'diver_id','groups','created','_password','diver']
    
    class edit_form_type(EditableForm):
        __model__ = Diver
        __omit_fields__ = ['id','author_id']
        
    class edit_form_type(EditableForm):
        __model__ = Diver
        
    class edit_filler_type(EditFormFiller):
        __model__ = Diver
    
    class table_type(TableBase):
        __model__ = Diver
    
    class table_filler_type(TableFiller):
        __model__ = Diver
    
